using System.Collections;
using AesEverywhere;
using UnityEngine;
using UnityEngine.Networking;

public class Api : MonoBehaviour
{
	public delegate void CallbackDelegate(string text);

	public static string endpoint = "https://onhit.dev/game/tac/";

	public static CallbackDelegate callbackDelegate;

	public static void AddLog(CampaignModel campaignData)
	{
		AES256 aES = new AES256();
		Post("/api.php", aES.Encrypt(JsonUtility.ToJson(DO.Campaign), Config.encryptionKey), null);
	}

	public static IEnumerator Post(string path, string jsonString, CallbackDelegate callback)
	{
		WWWForm wWWForm = new WWWForm();
		wWWForm.AddField("data", jsonString);
		UnityWebRequest www = UnityWebRequest.Post(endpoint + path, wWWForm);
		yield return www.SendWebRequest();
		if (www.result != UnityWebRequest.Result.ConnectionError && www.result != UnityWebRequest.Result.ProtocolError)
		{
			callback?.Invoke(www.downloadHandler.text);
		}
	}

	public static IEnumerator Get(string path, CallbackDelegate callback)
	{
		UnityWebRequest www = UnityWebRequest.Get(endpoint + path);
		yield return www.SendWebRequest();
		if (www.result != UnityWebRequest.Result.ConnectionError && www.result != UnityWebRequest.Result.ProtocolError)
		{
			callback?.Invoke(www.downloadHandler.text);
		}
	}
}
